package top.lyjwn.ajui.test;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import top.lyjwn.ajui.R;
import top.lyjwn.ajui.data.Message;
import top.lyjwn.ajui.widget.recycler.base.BaseViewHolder;
import top.lyjwn.ajui.widget.recycler.base.RecyclerBaseListAdapter;

/**
 * 聊天内容适配器
 *
 * @author luoyuji
 */
public class ChatContentAdapter extends RecyclerBaseListAdapter<Message> {

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_content, parent, false);
        ChatContentAdapter.ChatContentHolder holder = new ChatContentAdapter.ChatContentHolder(inflate);
        holder.setContext(parent.getContext());
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        ChatContentHolder chatContentHolder = (ChatContentHolder) holder;
        if (TList.get(position) != null) {
                chatContentHolder.setMessage(TList.get(position));
        }
    }

    static class ChatContentHolder extends BaseViewHolder {

        private LinearLayout opposite_message_container;
        private LinearLayout mine_message_container;
        private TextView opposite_message_text;
        private TextView mine_message_text;

        public ChatContentHolder(View view) {
            super(view);
            opposite_message_container = view.findViewById(R.id.opposite_message_container);
            mine_message_container = view.findViewById(R.id.mine_message_container);
            opposite_message_text = view.findViewById(R.id.opposite_message_text);
            mine_message_text = view.findViewById(R.id.mine_message_text);
        }

        public void setMessage(Message message) {
            if (message.getSender().getId().equals(UserUtil.getUser().getId())) {
                opposite_message_container.setVisibility(View.GONE);
                mine_message_container.setVisibility(View.VISIBLE);
                mine_message_text.setText(message.getContent().getText());
            } else {
                mine_message_container.setVisibility(View.GONE);
                opposite_message_container.setVisibility(View.VISIBLE);
                opposite_message_text.setText(message.getContent().getText());
            }
        }
    }
}
